package com.cmy.b_claculator.ui.details

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.cmy.b_claculator.R
import com.cmy.b_claculator.databinding.FragmentWorkDetailsBinding
import com.example.baselibrary.app.base.BaseFragment
import com.example.baselibrary.app.ext.*
import com.example.baselibrary.app.weight.recyclerview.SpaceItemDecoration
import com.kingja.loadsir.core.LoadService
import kotlinx.android.synthetic.main.include_list.*
import kotlinx.android.synthetic.main.include_recyclerview.*
import kotlinx.android.synthetic.main.include_toolbar.*
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

class WorkDetailsFragment : BaseFragment<WorkDetailsViewModel,FragmentWorkDetailsBinding>(){
    override fun layoutId(): Int  = R.layout.fragment_work_details
    //适配器
    private val detailsAdapter: DetailsAdapter by lazy { DetailsAdapter(arrayListOf()) }
    //界面状态管理者
    private lateinit var loadsir: LoadService<Any>

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewModel = mViewModel
        arguments?.let {
            mViewModel.id = it.getLong("ID")
        }

        toolbar.run {
            initClose("详情") {
                nav().navigateUp()
            }
            inflateMenu(R.menu.main_ment)
            setOnMenuItemClickListener {
                when (it.itemId) {
                    R.id.todo_add -> {
                        nav().navigateAction(R.id.nav_add_work)
                    }
                }
                true
            }
        }


        //状态页配置 swipeRefresh参数表示你要包裹的布局
        loadsir = loadServiceInit(swipeRefresh) {
            //点击错误重试时触发的操作
            loadsir.showLoading()
            mViewModel.initData()
            swipeRefresh.isRefreshing = false
        }

        //初始化recyclerView
        recyclerView.init(LinearLayoutManager(context), detailsAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, ConvertUtils.dp2px(8f)))
//            it.initFooter(SwipeRecyclerView.LoadMoreListener {
//                //触发加载更多时请求数据
////                requestViewModel.getTodoData(false)
//            })
            //初始化FloatingActionButton
            it.initFloatBtn(floatbtn)
        }
        //初始化 SwipeRefreshLayout
        swipeRefresh.init {
            //触发刷新监听时请求数据
            mViewModel.initData()
            swipeRefresh.isRefreshing = false
        }
        detailsAdapter.run {
            setOnItemClickListener { _, _, position ->
                nav().navigateAction(R.id.nav_add_work,
                    Bundle().apply {
                        putLong("ID",detailsAdapter.data[position].id)
                    })
            }
        }

        mViewModel.initData()

    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.listDataState.observe(viewLifecycleOwner,  {
            if (it.isNullOrEmpty()){
                loadsir.showEmpty()
            }else{
                detailsAdapter.data = it
                detailsAdapter.notifyDataSetChanged()
                loadsir.showSuccess()
            }
        })
    }

}